<template> <!-- vue实例外创建 -->
  <div>
    <div class="login">
      <h1>登录</h1>
      <el-form ref="loginform" :rules="rules" :model="form" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input placeholder="请输入用户名" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <!-- 使用show-password属性即可得到一个可切换显示隐藏的密码框 -->
          <el-input placeholder="请输入密码" v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form>
    </div>
  </div>
</template>
<script>
import { Login } from '@/api/user';
export default {
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请选择密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs['loginform'].validate( async(valid) => {
        if (valid) {
          let {data,meta} =await Login(this.form)
          // 什么情况跳转 
          if(meta.status!=200) return this.$message.error(meta.msg)
          localStorage.setItem('admin_token',data.token)
        this.$router.push('/')
        }
      });
    }
  }
}
</script>
<style scoped>
.login {
  width: 400px;
  height: 400px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border: 1px solid #000;
  border-radius: 15px;
  padding: 15px;
  text-align: center;
}

h1 {
  text-align: center;
}
</style>